from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU, GoodsCategory, SPU
from meiduo_admin.serializer.skus import SKUGoodsSerializer, GoodsCategorySerializer, SPUSpecificationSerializer
from meiduo_admin.utils import UserPageNum


class SKUGoodsView(ModelViewSet):
    # 继承五个拓展类和genericAPIciew
    serializer_class = SKUGoodsSerializer
    # 指定分页
    pagination_class = UserPageNum

    def  get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword == '' or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(name=keyword)

    def categories(self,request):
        good = GoodsCategory.objects.filter(id__gte=115)
        ser = GoodsCategorySerializer(good,many=True)
        return Response(ser.data)

    def specs(self,request,pk):

        data = SPU.objects.get(id=pk)
        spec = data.specs.all()
        ser = SPUSpecificationSerializer(spec,many=True)
        return Response(ser.data)



